package conan.presentation;

import conan.domain.Controller;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class OpretKundeFrame
    extends JFrame
{
    private Controller control;
    private JButton opretKundeButton = new JButton();
    private JTextField jTextField1 = new JTextField();
    private JLabel jLabelIndtastKundenavn = new JLabel();
    private JButton jButtonFortryd = new JButton();
    private JLabel jLabelStatus = new JLabel();

    public OpretKundeFrame()
    {
        try
        {
            jbInit();
            JFrame frame = this;
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize = frame.getSize();
            if (frameSize.height > screenSize.height)
            {
                frameSize.height = screenSize.height;
            }
            if (frameSize.width > screenSize.width)
            {
                frameSize.width = screenSize.width;
            }
            frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
            addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        luk();
                    }
                });
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void jbInit()
        throws Exception
    {
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(254, 146));
        this.setTitle("Opret ny kunde");
        this.setResizable(false);
        opretKundeButton.setText("Opret");
        opretKundeButton.setBounds(new Rectangle(25, 85, 80, 25));
        opretKundeButton.setSize(new Dimension(80, 25));
        opretKundeButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    opretKundeButton_actionPerformed(e);
                }
            });
        jTextField1.setBounds(new Rectangle(25, 30, 200, 25));
        jTextField1.setSize(new Dimension(200, 25));
        jLabelIndtastKundenavn.setText("Indtast kundenavn:");
        jLabelIndtastKundenavn.setBounds(new Rectangle(25, 5, 200, 25));
        jLabelIndtastKundenavn.setFont(new Font("Tahoma", 1, 11));
        jLabelIndtastKundenavn.setAlignmentX((float) 11.0);
        jLabelIndtastKundenavn.setHorizontalAlignment(SwingConstants.LEFT);
        jButtonFortryd.setText("Fortryd");
        jButtonFortryd.setBounds(new Rectangle(145, 85, 80, 25));
        jButtonFortryd.setSize(new Dimension(80, 25));
        jButtonFortryd.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    lukButton_actionPerformed(e);
                }
            });
        jLabelStatus.setText("");
        jLabelStatus.setBounds(new Rectangle(25, 60, 200, 20));
        this.getContentPane().add(jLabelStatus, null);
        this.getContentPane().add(jButtonFortryd, null);
        this.getContentPane().add(jLabelIndtastKundenavn, null);
        this.getContentPane().add(jTextField1, null);
        this.getContentPane().add(opretKundeButton, null);
    }

    public void aabn(Controller control)
    {
        this.control = control;
        this.control.useCaseStart();
        this.setVisible(true);
    }

    private void opretKundeButton_actionPerformed(ActionEvent e)
    {
        jLabelStatus.setText("");
        if (jTextField1.getText().length() > 0 && control.gemNewKunde(jTextField1.getText()))
        {
            this.setVisible(false);
            ((JFrame) this).dispose();
        }
        else
        {
            jLabelStatus.setText("Fejl, kunden blev ikke gemt.");
        }

    }

    private void lukButton_actionPerformed(ActionEvent e)
    {
        luk();
        ((JFrame) this).dispose();
    }

    private void luk()
    {
        control.useCaseAfslutUdenCommit();
    }
}
